package com.shangma.poi;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.word.Word07Writer;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.awt.*;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

public class WordOutput {

    public static void main(String[] args) {

//        Word07Writer writer = new Word07Writer();
//
//// 添加段落（标题）
//        writer.addText(new Font("方正小标宋简体", Font.BOLD, 22), "我是第一部分", "我是第二部分");
//// 添加段落（正文）
//        writer.addText(new Font("宋体", Font.PLAIN, 22), "我是正文第一部分", "我是正文第二部分");
//// 写出到文件
//        writer.flush(FileUtil.file("e:/wordWrite.docx"));
//// 关闭
//        writer.close();
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("name","谷丰硕");
            map.put("cardId","410329349349394394");
            map.put("reason","个人原因");
            map.put("date","2021-10-20");
            Writer out  = new OutputStreamWriter(new FileOutputStream("E:\\test10.docx"), "UTF-8");
            //freemarker.template.Configuration
            Configuration configuration = new Configuration();
            configuration.setDefaultEncoding("UTF-8");//设置编码
            //类加载器，设置xml模板文件的路径
            configuration.setClassForTemplateLoading(WordOutput.class, "/");
            //获取模板对象 传入模板名称
            Template t = configuration.getTemplate("test.xml");
            t.process(map, out);//模板中传入数据
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
